package com.gjh.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gjh.dao.cargo.ContractDao;
import com.gjh.dao.cargo.ContractProductDao;
import com.gjh.dao.cargo.ExtCproductDao;
import com.gjh.domain.cargo.*;
import com.gjh.service.cargo.ContractProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ContractProductServiceImpl implements ContractProductService{
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ExtCproductDao extCproductDao;

    @Override
    public void save(ContractProduct contractProduct) {
        //计算出小计金额
        Double price = contractProduct.getPrice();
        Integer cnumber = contractProduct.getCnumber();
        Double amount = price*cnumber;
        contractProduct.setAmount(amount);
        //获取合同信息，并添加货物个数和货物的总价格
        String contractId = contractProduct.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        contract.setProNum(contract.getProNum()+1);
        contract.setTotalAmount(contract.getTotalAmount()+amount);
        contractDao.updateByPrimaryKeySelective(contract);

        contractProductDao.insertSelective(contractProduct);
    }

    @Override
    public void update(ContractProduct contractProduct) {
        //获取货物修改之前的对象及信息
        String productId = contractProduct.getId();
        ContractProduct contractProduct_old = contractProductDao.selectByPrimaryKey(productId);
        Double amount_old = contractProduct_old.getAmount();
        //计算新的总价
        Double price = contractProduct.getPrice();
        Integer cnumber = contractProduct.getCnumber();
        Double amount_new = price*cnumber;
        contractProduct.setAmount(amount_new);

        //获取合同id
        String contractId = contractProduct.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        contract.setTotalAmount(contract.getTotalAmount()-amount_old+amount_new);
        contractDao.updateByPrimaryKeySelective(contract);

        contractProductDao.updateByPrimaryKeySelective(contractProduct);
    }

    @Override
    public void delete(String id) {
        //        1、删除货物
//        2、删除此货物下的附件  delete from co_ext_cproduct where product_id=?
//        3、修改合同的货物数量 货物数量直接-1
//        4、修改合同的附件数量 附件数量-此货物下的附件数
//        5、修改合同的总金额   总金额-货物的小计金额-（此货物下所有附件的小计金额）
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        String contractId = contractProduct.getContractId();

        List<ExtCproduct> extCproducts = contractProduct.getExtCproducts();
        Double extTotalAmount = 0.0;//附件总金额
        for (ExtCproduct extCproduct : extCproducts) {
            extTotalAmount += extCproduct.getAmount();//获取每个附加的小计金额，并加到总金额里
            extCproductDao.deleteByPrimaryKey(extCproduct.getId());//删除附加
        }

        Contract contract = contractDao.selectByPrimaryKey(contractId);
        contract.setProNum(contract.getProNum()-1);
        contract.setExtNum(contract.getExtNum()-extCproducts.size());
        contract.setTotalAmount(contract.getTotalAmount()-contractProduct.getAmount()-extTotalAmount);
        contractDao.updateByPrimaryKeySelective(contract);//更新到表中

        contractProductDao.deleteByPrimaryKey(id);
    }

    @Override
    public ContractProduct findById(String id) {
        return contractProductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ContractProductExample example, int page, int size,String contractId) {
        PageHelper.startPage(page,size);
        List<ContractProduct> list = contractProductDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    public void saveList(List<ContractProduct> productList) {
        for (ContractProduct contractProduct : productList) {
            this.save(contractProduct);//保存货物数据
        }
    }
}
